package com.ohos.connect.devicecloud.iot.coap;

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.Utils;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.eclipse.californium.core.coap.Request;
import org.eclipse.californium.elements.exception.ConnectorException;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * CoAP客户端工具类，用于与远程CoAP服务通信
 */
@Component
public class CoapClientUtil {

    /**
     * 发送CoAP GET请求
     *
     * @param uri CoAP服务URI
     * @return 响应字符串
     */
    public String get(String uri) {
        CoapClient client = new CoapClient(uri);
        try {
            CoapResponse response = client.get();
            if (response != null) {
                if (response.isSuccess()) {
                    return response.getResponseText();
                } else {
                    return "Request failed: " + response.getCode();
                }
            } else {
                return "No response";
            }
        } catch (ConnectorException | IOException e) {
            return "Error: " + e.getMessage();
        } finally {
            client.shutdown();
        }
    }

    /**
     * 发送CoAP POST请求
     *
     * @param uri     CoAP服务URI
     * @param payload 请求负载
     * @return 响应字符串
     */
    public String post(String uri, String payload) {
        CoapClient client = new CoapClient(uri);
        try {
            CoapResponse response = client.post(payload, MediaTypeRegistry.TEXT_PLAIN);
            if (response != null) {
                if (response.isSuccess()) {
                    return response.getResponseText();
                } else {
                    return "Request failed: " + response.getCode();
                }
            } else {
                return "No response";
            }
        } catch (ConnectorException | IOException e) {
            return "Error: " + e.getMessage();
        } finally {
            client.shutdown();
        }
    }

    /**
     * 发送CoAP PUT请求
     *
     * @param uri     CoAP服务URI
     * @param payload 请求负载
     * @return 响应字符串
     */
    public String put(String uri, String payload) {
        CoapClient client = new CoapClient(uri);
        try {
            CoapResponse response = client.put(payload, MediaTypeRegistry.TEXT_PLAIN);
            if (response != null) {
                if (response.isSuccess()) {
                    return response.getResponseText();
                } else {
                    return "Request failed: " + response.getCode();
                }
            } else {
                return "No response";
            }
        } catch (ConnectorException | IOException e) {
            return "Error: " + e.getMessage();
        } finally {
            client.shutdown();
        }
    }

    /**
     * 发送CoAP DELETE请求
     *
     * @param uri CoAP服务URI
     * @return 响应字符串
     */
    public String delete(String uri) {
        CoapClient client = new CoapClient(uri);
        try {
            CoapResponse response = client.delete();
            if (response != null) {
                if (response.isSuccess()) {
                    return response.getResponseText();
                } else {
                    return "Request failed: " + response.getCode();
                }
            } else {
                return "No response";
            }
        } catch (ConnectorException | IOException e) {
            return "Error: " + e.getMessage();
        } finally {
            client.shutdown();
        }
    }

    /**
     * 发送自定义CoAP请求
     *
     * @param uri     CoAP服务URI
     * @param request 自定义请求对象
     * @return 响应字符串
     */
    public String sendRequest(String uri, Request request) {
        CoapClient client = new CoapClient(uri);
        try {
            CoapResponse response = client.advanced(request);
            if (response != null) {
                System.out.println("Response code: " + response.getCode());
                System.out.println("Payload: " + response.getResponseText());
                System.out.println("Full response: " + Utils.prettyPrint(response));
                return response.getResponseText();
            } else {
                return "No response";
            }
        } catch (ConnectorException | IOException e) {
            return "Error: " + e.getMessage();
        } finally {
            client.shutdown();
        }
    }
}